package com.lwq.project.interceptor;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

/**
* @author 作者 linweiqiang
* @version 创建时间：2017年11月7日 上午10:33:32
* 类说明
*/
public class MyExceptionHandle implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		//判断是否是ajax请求
		if (!(request.getHeader("accept").indexOf("application/json") > -1 || (request
                .getHeader("X-Requested-With") != null && request.getHeader(
                "X-Requested-With").indexOf("XMLHttpRequest") > -1))) {
			//不是ajax请求
		}else{
			response.setContentType("application/json;charset=UTF-8");
			try {
				PrintWriter writer = response.getWriter() ;
				Map<String ,Object > map = new HashMap<String ,Object >();
				if(ex instanceof MyException){
					MyException my = (MyException) ex;
					map.put("msg", my.getMsg());
					map.put("namne", my.getName());
					map.put("value", my.getValue());
					return null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}
